unit fposdialogs;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Classes,
  fpg_base, fpg_main, fpg_form, fpg_label, fpg_button, fpg_edit, fpg_imgfmt_bmp,
  fpg_dialogs, fpg_resources, fpg_panel, fpg_listbox, fpg_grid,
  fposUtils, basereports, StarSerial;

type

  { TRegisterClientForm }

  TRegisterClientForm = class(TfpgForm)
  public
    {@VFD_HEAD_BEGIN: RegisterClientForm}
    Label1: TfpgLabel;
    TerminalNameEdit: TfpgEdit;
    Label2: TfpgLabel;
    ServerAddressEdit: TfpgEdit;
    Label3: TfpgLabel;
    ServerPortEdit: TfpgEdit;
    Label4: TfpgLabel;
    Label5: TfpgLabel;
    OKButton: TfpgButton;
    CancelButton: TfpgButton;
    {@VFD_HEAD_END: RegisterClientForm}
    Logo: TfpgImage;
    procedure PaintLogo(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure PortEditExit(Sender: TObject);
  public
    procedure AfterCreate; override;
  end;

  { TLogonForm }

  TLogonForm = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: LogonForm}
    MainPanel: TfpgPanel;
    StaffListBox: TfpgListBox;
    PasswordEdit: TfpgEdit;
    Label2: TfpgLabel;
    Label3: TfpgLabel;
    CashEdit: TfpgEditFloat;
    LogonButton: TfpgButton;
    Label1: TfpgLabel;
    Label4: TfpgLabel;
    {@VFD_HEAD_END: LogonForm}
    procedure FormKeypress(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
    procedure CashInputKeyPress(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState; var Consumed: boolean);
    procedure LogonButtonClick(Sender: TObject);
    function CheckCashValue: Boolean;
  public
    procedure AfterCreate; override;
    procedure AddStaffMember(aname: string);
    property Cash: TfpgEditFloat read CashEdit;
  end;


  { TLogoutForm }

  TLogoutForm = class(TfpgForm)
  private
    ClockTimer: TfpgTimer;
  public
    {@VFD_HEAD_BEGIN: LogoutForm}
    MainPanel: TfpgPanel;
    EndShiftLabel: TfpgLabel;
    TimeLabel: TfpgLabel;
    StaffNameLabel: TfpgLabel;
    StartTimeLabel: TfpgLabel;
    CashStartLabel: TfpgLabel;
    CashInLabel: TfpgLabel;
    CashinDrawerLabel: TfpgLabel;
    CashEdit: TfpgEdit;
    LogoutButton: TfpgButton;
    ZTotalGrid: TfpgStringGrid;
    CancelButton: TfpgButton;
    {@VFD_HEAD_END: LogoutForm}
  public
    procedure AfterCreate; override;
    destructor Destroy; override;
    procedure OnClockTimer(Sender: TObject);
    procedure LogoutButtonClick(Sender: TObject);
    procedure PrintReport;
  end;


  { TSideBar }

  TSideBar = class(TfpgForm)
  public
    {@VFD_HEAD_BEGIN: SideBar}
    EndShiftButton: TfpgButton;
    ReportsButton: TfpgButton;
    RefundButton: TfpgButton;
    CancelButton: TfpgButton;
    BackButton: TfpgButton;
    {@VFD_HEAD_END: SideBar}
  public
    procedure AfterCreate; override;
    procedure HideBar(Sender: TObject);
    procedure AdjustHeight( aheight: TfpgCoord );
    procedure ReportsButtonClick(Sender: TObject);
    procedure EndShiftButtonClick(Sender: TObject);
  end;

  { TReportButtons }

  TReportButtons = class(TfpgForm)
  private
    {@VFD_HEAD_BEGIN: ReportButtons}
    ShiftReportButton: TfpgButton;
    SaleListReportButton: TfpgButton;
    CancelButton: TfpgButton;
    {@VFD_HEAD_END: ReportButtons}
    procedure ShiftReportClick(Sender: TObject);
    procedure SalesReportClick(Sender: TObject);
    procedure CloseClick(Sender: TObject);
  public
    procedure AfterCreate; override;
  end;

  { TContinueShiftDlg }

  TContinueShiftDlg = class(TfpgForm)
  public
    {@VFD_HEAD_BEGIN: ContinueShiftDlg}
    MainPanel: TfpgPanel;
    TitleLabel: TfpgLabel;
    MessageLabel: TfpgLabel;
    ContinueShiftButton: TfpgButton;
    TerminateShiftButton: TfpgButton;
    OperatorLabel: TfpgLabel;
    ShiftStartLabel: TfpgLabel;
    ClosetimeLabel: TfpgLabel;
    {@VFD_HEAD_END: ContinueShiftDlg}
  public
    procedure AfterCreate; override;
    procedure ContinueClick(Sender: TObject);
    procedure RestartClick(Sender: TObject);
  end;

{@VFD_NEWFORM_DECL}

var
  SideBar: TSideBar;
  ReportButtons: TReportButtons;

implementation

uses
  fpguiTerm, fposclientcore, fposclientdb;

{@VFD_NEWFORM_IMPL}

//==============================================================================
//  TContinueShiftDlg - handles detection of interrupted shift
//==============================================================================

procedure TContinueShiftDlg.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: ContinueShiftDlg}
  Name := 'ContinueShiftDlg';
  SetPosition(52, 173, 902, 629);
  WindowTitle := 'ContinueShiftDlg';
  Hint := '';
  BackgroundColor := clWhite;
  WindowAttributes := [waBorderless];
  Width := fpgApplication.ScreenWidth;
  Height := fpgApplication.ScreenHeight;

  MainPanel := TfpgPanel.Create(self);
  with MainPanel do
  begin
    Name := 'MainPanel';
    SetPosition(0, 36, 744, 508);
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
    SetPosition((fpgApplication.ScreenWidth-600) div 2, (fpgApplication.ScreenHeight-470) div 2, 744, 508);
  end;

  TitleLabel := TfpgLabel.Create(MainPanel);
  with TitleLabel do
  begin
    Name := 'TitleLabel';
    SetPosition(172, 48, 416, 35);
    Alignment := taCenter;
    FontDesc := '#Headline';
    Hint := '';
    Text := 'Unfinished Shift Found';
    AutoSize := false;
  end;

  MessageLabel := TfpgLabel.Create(MainPanel);
  with MessageLabel do
  begin
    Name := 'MessageLabel';
    SetPosition(60, 108, 540, 32);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'FirePOS appears to have terminated uncleanly during a shift.';
    WrapText := True;
  end;

  ContinueShiftButton := TfpgButton.Create(MainPanel);
  with ContinueShiftButton do
  begin
    Name := 'ContinueShiftButton';
    SetPosition(60, 248, 620, 82);
    Text := 'Continue this shift';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 3;
    OnClick := @ContinueClick;
  end;

  TerminateShiftButton := TfpgButton.Create(MainPanel);
  with TerminateShiftButton do
  begin
    Name := 'TerminateShiftButton';
    SetPosition(60, 364, 620, 82);
    Text := 'Start a new shift';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 4;
    OnClick := @RestartClick;
  end;

  OperatorLabel := TfpgLabel.Create(MainPanel);
  with OperatorLabel do
  begin
    Name := 'OperatorLabel';
    SetPosition(60, 148, 620, 24);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Operator:  ';
  end;

  ShiftStartLabel := TfpgLabel.Create(MainPanel);
  with ShiftStartLabel do
  begin
    Name := 'ShiftStartLabel';
    SetPosition(60, 176, 624, 24);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Shift start:  ';
  end;

  ClosetimeLabel := TfpgLabel.Create(MainPanel);
  with ClosetimeLabel do
  begin
    Name := 'ClosetimeLabel';
    SetPosition(60, 204, 616, 32);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Shift interrupted at:  ';
  end;

  {@VFD_BODY_END: ContinueShiftDlg}
  {%endregion}
end;

procedure TContinueShiftDlg.ContinueClick ( Sender: TObject ) ;
begin
  ModalResult := mrOK;
  Self.Hide;
end;

procedure TContinueShiftDlg.RestartClick ( Sender: TObject ) ;
begin
  ModalResult := mrCancel;
  Self.Hide;
end;


//==============================================================================
//  TReportButtons
//==============================================================================
procedure TReportButtons.ShiftReportClick ( Sender: TObject ) ;
var
  Report: TShiftReport;
begin

end;

procedure TReportButtons.SalesReportClick ( Sender: TObject ) ;
var
  Report: TShiftReport;
begin
  Self.Hide;
  Report := TShiftReport.Create(nil);
  Report.FromTime := ClientDB.ShiftInfo.StartTime; //Round(ClientDB.ShiftInfo.StartTime *100) /100;
  Report.FromToPanel.Visible := false;
  Report.ToTime := DateTimeToJulianDateTime( Now + TimeAdjustment );
  Report.OnGoButtonClick(nil);
  Report.Show;
  Self.ModalResult := mrOK;
end;

procedure TReportButtons.CloseClick ( Sender: TObject ) ;
begin
  Self.Hide;
  ModalResult := mrCancel;
end;

procedure TReportButtons.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: ReportButtons}
  Name := 'ReportButtons';
  SetPosition(4, 0, 374, 92);
  WindowTitle := 'ReportButtons';
  Hint := '';
  WindowPosition := wpUser;
  WindowAttributes := [waBorderless];
  BackgroundColor := clBlue;

  ShiftReportButton := TfpgButton.Create(self);
  with ShiftReportButton do
  begin
    Name := 'ShiftReportButton';
    SetPosition(72, 4, 144, 80);
    Text := 'Shift Summary';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 1;
    OnClick := @ShiftReportClick;
  end;

  SaleListReportButton := TfpgButton.Create(self);
  with SaleListReportButton do
  begin
    Name := 'SaleListReportButton';
    SetPosition(224, 4, 144, 80);
    Text := 'Sale List';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 2;
    OnClick := @SalesReportClick;
  end;

  CancelButton := TfpgButton.Create(self);
  with CancelButton do
  begin
    Name := 'CancelButton';
    SetPosition(4, 4, 60, 80);
    Text := 'X';
    BackgroundColor := TfpgColor($FF0000);
    FontDesc := '#Label2';
    Hint := '';
    ImageName := '';
    TabOrder := 3;
    OnClick := @CloseClick;
  end;

  {@VFD_BODY_END: ReportButtons}
  {%endregion}
end;


//==============================================================================
//  TSideBar - main menu buttons
//==============================================================================

procedure TSideBar.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: SideBar}
  Name := 'SideBar';
  SetPosition(0, 62, 159, 557);
  WindowTitle := 'SideBar';
  Hint := '';
  WindowPosition := wpUser;
  SetPosition(fpgApplication.ScreenWidth-153, 0, 153, fpgApplication.ScreenHeight-10);
  WindowAttributes := [waBorderless];
  BackgroundColor := clBlue;

  EndShiftButton := TfpgButton.Create(self);
  with EndShiftButton do
  begin
    Name := 'EndShiftButton';
    SetPosition(8, 96, 144, 80);
    Text := 'End Shift';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 3;
    Onclick := @EndShiftButtonClick;
  end;

  ReportsButton := TfpgButton.Create(self);
  with ReportsButton do
  begin
    Name := 'ReportsButton';
    SetPosition(8, 4, 144, 80);
    Text := 'Reports';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 4;
    OnClick := @ReportsButtonClick;
  end;

  RefundButton := TfpgButton.Create(self);
  with RefundButton do
  begin
    Name := 'RefundButton';
    SetPosition(8, 356, 144, 80);
    Text := 'Refund';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 5;
    OnClick := @fposTerm.RefundClick;
  end;

  CancelButton := TfpgButton.Create(self);
  with CancelButton do
  begin
    Name := 'CancelButton';
    SetPosition(8, 444, 144, 80);
    Text := 'Cancel Sale';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 6;
    OnClick := @fposTerm.CancelSaleClick;
  end;

  BackButton := TfpgButton.Create(self);
  with BackButton do
  begin
    Name := 'BackButton';
    SetPosition(4, 0, 80, 24);
    Text := 'X';
    BackgroundColor := TfpgColor($FF0000);
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 7;
    SetPosition(4, Parent.Height - 84, 144, 60);
    OnClick := @HideBar;
  end;

  {@VFD_BODY_END: SideBar}
  {%endregion}
end;

procedure TSideBar.HideBar ( Sender: TObject ) ;
begin
  ModalResult := mrCancel;
end;

procedure TSideBar.AdjustHeight ( aheight: TfpgCoord ) ;
begin
  SetPosition(fposTerm.Width-158, 12, 155, aheight);
  BackButton.SetPosition(4, Height - 84, 144, 80);     ;
end;

procedure TSideBar.ReportsButtonClick ( Sender: TObject ) ;
begin
  ReportButtons := TReportButtons.Create(nil);
  ReportButtons.SetPosition(Self.Left-ReportButtons.Width, Self.Top + ReportsButton.Top-4, ReportButtons.Width, ReportButtons.Height);
  Self.BackgroundColor := clButtonFace;
  Self.Enabled := false;
  if ReportButtons.ShowModal = mrOK
     then Close;
  Self.BackgroundColor := clBlue;
  ReportButtons.Free;
  Self.Enabled := true;
end;

procedure TSideBar.EndShiftButtonClick(Sender: TObject);
begin
  fposTerm.EndShift;
  Self.ModalResult := mrOK;
end;


//==============================================================================
//  TLogoutForm
//==============================================================================

procedure TLogoutForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: LogoutForm}
  Name := 'LogoutForm';
  SetPosition(158, 244, 902, 558);
  WindowTitle := 'LogoutForm';
  Hint := '';
  WindowAttributes := [waBorderless];
  Width := fpgApplication.ScreenWidth;
  Height := fpgApplication.ScreenHeight;
  //OnKeyPress := @FormKeyPress;

  MainPanel := TfpgPanel.Create(self);
  with MainPanel do
  begin
    Name := 'MainPanel';
    SetPosition(0, 0, 744, 508);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
    SetPosition((fpgApplication.ScreenWidth-600) div 2, (fpgApplication.ScreenHeight-470) div 2, 744, 508);
  end;

  EndShiftLabel := TfpgLabel.Create(MainPanel);
  with EndShiftLabel do
  begin
    Name := 'EndShiftLabel';
    SetPosition(24, 28, 688, 36);
    Alignment := taCenter;
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Headline';
    Hint := '';
    Text := 'End Shift';
    AutoSize := false;
  end;

  TimeLabel := TfpgLabel.Create(MainPanel);
  with TimeLabel do
  begin
    Name := 'TimeLabel';
    SetPosition(30, 96, 296, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'TimeLabel';
    Autosize := true;
  end;

  StaffNameLabel := TfpgLabel.Create(MainPanel);
  with StaffNameLabel do
  begin
    Name := 'StaffNameLabel';
    SetPosition(30, 124, 340, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'StaffName';
    Autosize := true;
  end;

  StartTimeLabel := TfpgLabel.Create(MainPanel);
  with StartTimeLabel do
  begin
    Name := 'StartTimeLabel';
    SetPosition(30, 152, 304, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Start Time';
    Autosize := true;
  end;

  CashStartLabel := TfpgLabel.Create(MainPanel);
  with CashStartLabel do
  begin
    Name := 'CashStartLabel';
    SetPosition(30, 204, 356, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Cash at start of shift: ';
    Autosize := true;
  end;

  CashInLabel := TfpgLabel.Create(MainPanel);
  with CashInLabel do
  begin
    Name := 'CashInLabel';
    SetPosition(30, 236, 148, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Cash received:';
    Autosize := true;
  end;

  CashinDrawerLabel := TfpgLabel.Create(MainPanel);
  with CashinDrawerLabel do
  begin
    Name := 'CashinDrawerLabel';
    SetPosition(30, 268, 112, 16);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Cash in drawer:';
    Autosize := true;
  end;

  CashEdit := TfpgEdit.Create(MainPanel);
  with CashEdit do
  begin
    Name := 'CashEdit';
    SetPosition(156, 264, 96, 24);
    ExtraHint := '';
    FontDesc := '#Edit1';
    Hint := '';
    TabOrder := 8;
    Text := '';
  end;

  LogoutButton := TfpgButton.Create(MainPanel);
  with LogoutButton do
  begin
    Name := 'LogoutButton';
    SetPosition(303, 412, 115, 70);
    Anchors := [anLeft,anBottom];
    Text := 'Confirm';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 11;
    OnClick := @LogoutButtonClick;
  end;

  ZTotalGrid := TfpgStringGrid.Create(MainPanel);
  with ZTotalGrid do
  begin
    Name := 'ZTotalGrid';
    SetPosition(448, 92, 264, 388);
    Anchors := [anLeft,anRight,anTop,anBottom];
    BackgroundColor := TfpgColor($80000002);
    FontDesc := '#Grid';
    HeaderFontDesc := '#GridHeader';
    Hint := '';
    RowCount := 0;
    RowSelect := False;
    TabOrder := 13;
  end;

  CancelButton := TfpgButton.Create(MainPanel);
  with CancelButton do
  begin
    Name := 'CancelButton';
    SetPosition(32, 412, 115, 70);
    Text := 'Cancel';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    ModalResult := mrCancel;
    TabOrder := 11;
  end;

  {@VFD_BODY_END: LogoutForm}
  {%endregion}
  ClockTimer := TfpgTimer.Create(500);
  ClockTimer.OnTimer := @OnClockTimer;
  ClockTimer.Enabled := true;
end;

destructor TLogoutForm.Destroy;
begin
 ClockTimer.Free;
 inherited Destroy;
end;

procedure TLogoutForm.OnClockTimer ( Sender: TObject ) ;
begin
  TimeLabel.Text := FormatDateTime('dddddd hh:mm:ss', Now + TimeAdjustment);
end;

procedure TLogoutForm.LogoutButtonClick ( Sender: TObject ) ;
begin
  PrintReport;
  ModalResult := mrOK;
end;

procedure TLogoutForm.PrintReport;
var
r:integer;
newline:string;
begin
 ConnectPrinter('COM1');

 SerialPrinter.SendString(#27'='#1);  // Select printer
 SerialPrinter.Flush;
 SerialPrinter.SendString( #27#77 + #27#29#97#49 );  //set 17cpi, centre align
 SerialPrinter.SendString(  #27#105#49#49 + 'Mallaig Heritage Centre' + #27#105#48#48 + #10#10);
 SerialPrinter.SendString( #27#69 + 'Shift Summary' + #27#70 + #10#10);
 SerialPrinter.SendString( #27#29#97#48);  // Set left align

 SerialPrinter.SendString('Day: ' + FormatDateTime('dddd d mmm yyyy', Date) + #10);

 newline := StringOfChar( #32, 17);
 AlignInString( newline, #27#69 + 'Operator: ' + ClientDB.ShiftInfo.StaffName + #27#70, taLeftJustify);
 SerialPrinter.SendString(#10 + newline + #10#10);


 newline := StringOfChar( #32, 17);
 AlignInString( newline, 'Shift start: ' + FormatJulianDateTime('d mmm yyyy hh:mm', ClientDB.ShiftInfo.StartTime), taLeftJustify);
 SerialPrinter.SendString(newline + #10);

 newline := StringOfChar( #32, 17);
 AlignInString( newline, 'Shift end: ' + FormatDateTime('d mmm yyyy hh:mm', Now + TimeAdjustment), taLeftJustify);
 SerialPrinter.SendString(newline + #10#10);

 newline := StringOfChar( #32, 17);
 AlignInString( newline, 'Start cash: ', taLeftJustify);
 AlignInString( newline, FormatFloat('0.00', ClientDB.ShiftInfo.StartCash), taRightJustify);
 SerialPrinter.SendString(newline + #10);

 newline := StringOfChar( #32, 17);
 AlignInString( newline, 'Receipts total: ' , taLeftJustify);
 AlignInString( newline, FormatFloat('0.00', ClientDB.ShiftInfo.ReceivedCash), taRightJustify);
 SerialPrinter.SendString(newline + #10);

 newline := StringOfChar( #32, 17);
 AlignInString( newline, 'Cash in drawer: ' , taLeftJustify);
 AlignInString( newline, CashEdit.Text, taRightJustify);
 SerialPrinter.SendString(newline + #10#10);

 SerialPrinter.SendString(#27#45#49 + 'Department                Sales         Subtotal' + #27#45#48 + #10);

 for r := 0 to ZTotalGrid.RowCount-1 do
   begin
     newline := StringOfChar( #32, 17);
     AlignInString( newline, ZTotalGrid.Cells[0, r] , taLeftJustify);
     Insert(ZTotalGrid.Cells[1, r], newline, 31-Length(ZTotalGrid.Cells[1, r]));
     newline := Copy(newline, 1, 48-Length(ZTotalGrid.Cells[2, r])) + ZTotalGrid.Cells[2, r];
     PrintReceiptLine( newline );
   end;
 SerialPrinter.SendString(StringOfChar('-', 17));
 SerialPrinter.SendString(#10#27#100#50); //cut
 SerialPrinter.Flush;

 SerialPrinter.Free;
 SerialPrinter := nil;
end;



//==============================================================================
//  TLogonForm
//==============================================================================

procedure TLogonForm.FormKeypress ( Sender: TObject; var KeyCode: word;
  var ShiftState: TShiftState; var Consumed: boolean ) ;
  var
    num: Extended;
  begin
  if KeyCode=keyF10 then ModalResult := mrCancel
  else if KeyCode=keyEnter then LogonButtonClick(nil)
  else begin
  {case KeyCode of
         keyP0, 48: num := 0.00;
         keyP1, 49: num := 0.01;
         keyP2, 50: num := 0.02;
         keyP3, 51: num := 0.03;
         keyP4, 52: num := 0.04;
         keyP5, 53: num := 0.05;
         keyP6, 54: num := 0.06;
         keyP7, 55: num := 0.07;
         keyP8, 56: num := 0.08;
         keyP9, 57: num := 0.09;
    end;
    if KeyCode=keyBackSpace
       then CashEdit.Value := CashEdit.Value/10
    else CashEdit.Value := (CashEdit.Value * 10) + num;
    KeyCode := keyEnd; }
  end;
end;

procedure TLogonForm.CashInputKeyPress ( Sender: TObject; var KeyCode: word;
  var ShiftState: TShiftState; var Consumed: boolean ) ;
begin
   //Consumed := true;
end;

procedure TLogonForm.LogonButtonClick ( Sender: TObject ) ;
begin
  if StaffListBox.FocusItem<0 then Exit;
  ClientDB.DataDB.RecNo := StaffListBox.FocusItem+1;
  if ClientDB.DataDB.FieldByName('pass').AsString=''
   then begin if CheckCashValue then ModalResult := mrOK; end
  else begin
         if CheckPass(PasswordEdit.Text)=ClientDB.DataDB.FieldByName('pass').AsString
            then ModalResult := mrOK
         else ShowMessage('Invalid password', 'Staff logon', true);
       end;
end;

function TLogonForm.CheckCashValue: Boolean;
var
  n: double;
begin
  Result := false;
  CashEdit.Text := Trim( CashEdit.Text );
  if CashEdit.Text ='' then begin CashEdit.Text := '0'; Result := true; Exit; end;
  if TryStrToFloat(CashEdit.Text, n) then Result := true
  else begin
         CashEdit.SetFocus;
         ShowMessage('Invalid cash amount', 'Error');
       end;
end;

procedure TLogonForm.AfterCreate;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: LogonForm}
  Name := 'LogonForm';
  SetPosition(202, 240, 902, 562);
  WindowTitle := 'LogonForm';
  Hint := '';
  WindowAttributes := [waBorderless];
  Width := fpgApplication.ScreenWidth;
  Height := fpgApplication.ScreenHeight;
  OnKeyPress := @FormKeyPress;

  MainPanel := TfpgPanel.Create(self);
  with MainPanel do
  begin
    Name := 'MainPanel';
    SetPosition(0, 0, 740, 508);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := '';
    SetPosition((fpgApplication.ScreenWidth-600) div 2, (fpgApplication.ScreenHeight-470) div 2, 612, 470);
  end;

  StaffListBox := TfpgListBox.Create(MainPanel);
  with StaffListBox do
  begin
    Name := 'StaffListBox';
    SetPosition(40, 84, 256, 356);
    FontDesc := '#List';
    Hint := '';
    TabOrder := 1;
  end;

  PasswordEdit := TfpgEdit.Create(MainPanel);
  with PasswordEdit do
  begin
    Name := 'PasswordEdit';
    SetPosition(336, 112, 240, 24);
    ExtraHint := '';
    FontDesc := '#Label1';
    Hint := '';
    TabOrder := 2;
    Text := '';
    PasswordMode := true;
  end;

  Label2 := TfpgLabel.Create(MainPanel);
  with Label2 do
  begin
    Name := 'Label2';
    SetPosition(336, 84, 348, 24);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Type password or swipe ID';
  end;

  Label3 := TfpgLabel.Create(MainPanel);
  with Label3 do
  begin
    Name := 'Label3';
    SetPosition(336, 160, 336, 24);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Cash in drawer';
  end;

  CashEdit := TfpgEditFloat.Create(MainPanel);
  with CashEdit do
  begin
    Name := 'CashEdit';
    SetPosition(340, 188, 120, 24);
    Decimals := 2;
    FixedDecimals := True;
    FontDesc := '#Label1';
    Hint := '';
    ShowThousand := False;
    TabOrder := 5;
    Value := 0;
    OnKeyPress := @CashInputKeyPress;
  end;

  LogonButton := TfpgButton.Create(MainPanel);
  with LogonButton do
  begin
    Name := 'LogonButton';
    SetPosition(340, 344, 108, 52);
    Text := 'Start shift';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 6;
    OnClick := @LogonButtonClick;
  end;

  Label1 := TfpgLabel.Create(MainPanel);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(20, 16, 568, 36);
    Alignment := taCenter;
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Headline';
    Hint := '';
    Text := 'Start Shift';
    AutoSize := false;
  end;

  Label4 := TfpgLabel.Create(MainPanel);
  with Label4 do
  begin
    Name := 'Label4';
    SetPosition(340, 408, 288, 24);
    BackgroundColor := TfpgColor($FFFBF0);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Close FirePOS - F10';
  end;

  {@VFD_BODY_END: LogonForm}
  {%endregion}
end;

procedure TLogonForm.AddStaffMember ( aname: string ) ;
begin
  StaffListBox.Items.Add(aname);
  if StaffListBox.ItemCount>0 then StaffListBox.FocusItem := 0;
end;



//==============================================================================
//  TRegisterClientForm
//==============================================================================

procedure TRegisterClientForm.PaintLogo(Sender: TObject);
begin
  Canvas.DrawImage(44, 12, Logo);
end;

procedure TRegisterClientForm.OKButtonClick ( Sender: TObject ) ;
begin
  if fposClient.RegisterTerminal(TerminalNameEdit.Text, ServerAddressEdit.Text, StrToInt(ServerPortEdit.Text))
   then ModalResult := mrOK;
  Hide;
end;

procedure TRegisterClientForm.PortEditExit ( Sender: TObject ) ;
var
  i: Integer;
begin
  if not TryStrToInt(ServerPortEdit.Text, i)
     then ServerPortEdit.SetFocus;
  SetFullscreen(True);
end;

procedure TRegisterClientForm.AfterCreate;
var
  ImageData: TfpgResource;
begin
  {%region 'Auto-generated GUI code' -fold}
  {@VFD_BODY_BEGIN: RegisterClientForm}
  Name := 'RegisterClientForm';
  SetPosition(300, 150, 672, 327);
  WindowTitle := 'RegisterClientForm';
  Hint := '';
  OnPaint := @PaintLogo;

  Label1 := TfpgLabel.Create(self);
  with Label1 do
  begin
    Name := 'Label1';
    SetPosition(44, 48, 364, 46);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'This sales terminal must be registered with the server before it can be used.';
    WrapText := True;
  end;

  TerminalNameEdit := TfpgEdit.Create(self);
  with TerminalNameEdit do
  begin
    Name := 'TerminalNameEdit';
    SetPosition(44, 164, 368, 24);
    ExtraHint := '';
    FontDesc := '#Edit1';
    Hint := '';
    TabOrder := 1;
    Text := '';
  end;

  Label2 := TfpgLabel.Create(self);
  with Label2 do
  begin
    Name := 'Label2';
    SetPosition(44, 140, 364, 18);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Terminal name';
  end;

  ServerAddressEdit := TfpgEdit.Create(self);
  with ServerAddressEdit do
  begin
    Name := 'ServerAddressEdit';
    SetPosition(44, 220, 224, 24);
    ExtraHint := '';
    FontDesc := '#Edit1';
    Hint := '';
    TabOrder := 3;
    Text := 'localhost';
  end;

  Label3 := TfpgLabel.Create(self);
  with Label3 do
  begin
    Name := 'Label3';
    SetPosition(44, 200, 224, 18);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Server address';
  end;

  ServerPortEdit := TfpgEdit.Create(self);
  with ServerPortEdit do
  begin
    Name := 'ServerPortEdit';
    SetPosition(44, 276, 96, 24);
    ExtraHint := '';
    FontDesc := '#Edit1';
    Hint := '';
    TabOrder := 5;
    Text := '';
  end;

  Label4 := TfpgLabel.Create(self);
  with Label4 do
  begin
    Name := 'Label4';
    SetPosition(44, 256, 220, 18);
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Server port';
  end;

  Label5 := TfpgLabel.Create(self);
  with Label5 do
  begin
    Name := 'Label5';
    SetPosition(44, 92, 368, 50);
    Anchors := [anLeft,anRight,anTop];
    FontDesc := '#Label1';
    Hint := '';
    Text := 'Check that the terminal is running, and change the Server address and Server port below, if necessary.';
    WrapText := True;
  end;

  OKButton := TfpgButton.Create(self);
  with OKButton do
  begin
    Name := 'OKButton';
    SetPosition(472, 292, 80, 22);
    Text := 'OK';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    TabOrder := 8;
    OnClick := @OKButtonClick;
  end;

  CancelButton := TfpgButton.Create(self);
  with CancelButton do
  begin
    Name := 'CancelButton';
    SetPosition(588, 292, 80, 22);
    Text := 'Cancel';
    FontDesc := '#Label1';
    Hint := '';
    ImageName := '';
    ModalResult := mrCancel;
    TabOrder := 9;
  end;

  {@VFD_BODY_END: RegisterClientForm}
  {%endregion}
  ImageData := LazarusResources.Find('firepos_logo_137x30');
  Logo := nil;
  if ImageData<>nil
     then Logo := CreateImage_BMP(Pointer(ImageData.Value), Length(ImageData.Value));
{$IFDEF DEBUG}   writeln(Length(ImageData.Value)); {$ENDIF}
 if Assigned(Logo) then
    begin
      Logo.CreateMaskFromSample(0,0);
      Logo.UpdateImage;
    end;
end;

end.
